package guiservice.server.controls;

import guiservice.server.logic.ControlsManager;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

@Log4j
public class Window extends Control implements guiservice.common.controls.Window {

    private final int
            arcWidth = 10,
            arcHeight = 10,
            systemButtonSize = 16,
            systemButtonSizeWithOffset = 18;

    private static Image hide, window, close;

    private guiservice.common.controls.Button closeButton;

    @Getter
    @Setter
    private String title;

    private JWindow windowComponent;

    private boolean isFirstUpdate = true;

    public Window(long id, long ownerId) {
        super(id, ownerId, guiservice.common.controls.Window.class.getCanonicalName());
        hide = loadImage("hide_button.png");
        window = loadImage("window_button.png");
        close = loadImage("close_button.png");
        windowComponent = new JWindow();
    }

    public void initializeCloseButton(long buttonId) {
        closeButton = ControlsManager.getInstance().getControl(buttonId, guiservice.common.controls.Button.class);
        closeButton.setSize(systemButtonSize, systemButtonSize);
        closeButton.setPoint(-systemButtonSizeWithOffset * 2 - arcWidth / 2, -systemButtonSize - arcHeight / 2);
    }

    @Override
    public void setPoint(int x, int y) {
        super.setPoint(x, y);
        if (isFirstUpdate) {
            int w = getWidth();
            x = closeButton.getX();
            y = closeButton.getY();
            closeButton.setPoint(x + w, y);
            isFirstUpdate = false;
            log.debug(String.format("close button x = %d, y = %d", x + w, y));
        }
    }

    public long getCloseButtonId() {
        return closeButton.getId();
    }

    public Graphics getGraphics() {
        if (image == null) {
            if (getWidth() != 0 && getHeight() != 0 && getComponent() != null) {
                image = new BufferedImage(getWidth() - arcWidth, getHeight() - 7 * arcHeight / 2, BufferedImage.TYPE_4BYTE_ABGR);
            } else {
                return null;
            }
        }
        Graphics graphics = image.getGraphics();
        graphics.setColor(new Color(getBackGround()));
        graphics.fill3DRect(0, 0, getWidth() - arcWidth, getHeight() - 7 * arcHeight / 2, true);
        image.flush();
        return graphics;
    }

    public int getXOffset() {
        return arcWidth / 2;
    }

    public int getYOffset() {
        return 3 * arcHeight;
    }

    @Override
    public void draw(Graphics g, JPanel board) {
        g.setColor(Color.blue);
        int x = getX(), y = getY(), width = getWidth(), height = getHeight();
        g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
        g.setColor(new Color(getBackGround()));
        g.fill3DRect(x + arcWidth / 2, y + 3 * arcHeight, width - arcWidth, height - 7 * arcHeight / 2, true);
        image.flush();
        g.drawImage(image, x + getXOffset(), y + getYOffset(), board);

        g.setColor(Color.WHITE);
        g.drawString(title, x + arcWidth, y + 3 * arcHeight / 2);

        int localX = x + width - systemButtonSizeWithOffset * 4, localY = y + arcHeight / 2;
        g.drawImage(hide, localX, localY, board);
        localX += systemButtonSizeWithOffset;
        g.drawImage(window, localX, localY, board);
        localX += systemButtonSizeWithOffset;
        g.drawImage(close, localX, localY, board);
    }

    @Override
    public boolean isContainer() {
        return true;
    }

    protected Component getComponent() {
        return windowComponent;
    }

}
